#include "QTimeDelegate.h"
#include "QTimeModel.h"
#include <QPainter>

QTimeDelegate::QTimeDelegate(QTimeModel *m, QObject *parent)
    : QItemDelegate(parent)
    , _timeModel(m)
{
}

void QTimeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QString value = index.data(Qt::DisplayRole).toString();
    if (option.state.testFlag(QStyle::State_Selected)) {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor(0, 145, 255));
        const QRect& bgRect = option.rect.adjusted(0, 1, 0, -1);
        painter->drawRoundedRect(bgRect, 3, 3);
        painter->setPen(QColor(255, 255, 255));
        painter->drawText(option.rect, Qt::AlignCenter, value);
        painter->restore();
        drawFocus(painter, option, option.rect);
    }
    else if (!option.state.testFlag(QStyle::State_Enabled)) {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor(249, 249, 249));
        const QRect& bgRect = option.rect.adjusted(0, 1, 0, -1);
        painter->drawRect(bgRect);
        painter->setPen(QColor(220, 220, 220));
        painter->drawText(option.rect, Qt::AlignCenter, value);
        painter->restore();
    }
    //else if (value.toInt() == _timeModel->value()) {
    //    painter->save();
    //    painter->setRenderHint(QPainter::Antialiasing);
    //    painter->setPen(Qt::NoPen);
    //    painter->setBrush(QColor(0, 145, 255));
    //    const QRect& bgRect = option.rect.adjusted(0, 1, 0, -1);
    //    painter->drawRoundedRect(bgRect, 3, 3);
    //    painter->setBrush(QColor(255, 255, 255));
    //    painter->drawRoundedRect(option.rect.adjusted(1, 2, -1, -2), 2, 2);
    //    painter->setPen(QColor(0, 145, 255));
    //    painter->drawText(option.rect, Qt::AlignCenter, value);
    //    painter->restore();
    //    drawFocus(painter, option, option.rect);
    //}
    else {
        QItemDelegate::paint(painter, option, index);
    }
}
